package com.aurora.springbootmybatis.controller;

import com.aurora.springbootmybatis.entity.Student;
import com.aurora.springbootmybatis.service.StudentService;
import com.aurora.springbootmybatis.utils.Result;
import com.github.pagehelper.PageInfo;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/bsns/student"})
public class StudentController {
    StudentService studentService;

    public StudentController() {
    }

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * 根据学生id查询学生信息
     * @param id
     * @return
     */
    @GetMapping({"/{id}"})
    public Result<Student> getStudent(@PathVariable("id") int id) {
        Student student = this.studentService.findStuById(id);
        return student != null ? Result.success("200", "查询成功", student) : Result.error("202", "查询失败");
    }

    /**
     * 查询所有学生信息
     * @return
     */
    @GetMapping({"/all"})
    public Result getAll() {
        return Result.success(this.studentService.findAll());
    }

    /**
     * 分页查询所有学生信息
     * @param student
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping({"/allPage/{pageNum}/{pageSize}"})
    public Result getAllByPage(Student student, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<Student> pageInfo = this.studentService.findAllByPage(student, pageNum, pageSize);
        System.out.println("分页信息：" + pageInfo);
        return Result.success(pageInfo);
    }

    /**
     * 新增一条学生信息
     * @param student
     * @return
     */
    @PostMapping({"/add"})
    public Result addStudent(Student student) {
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        Boolean result = this.studentService.saveStu(student);
        return result ? Result.success("200", "添加学生信息成功", student) : Result.error("202", "添加学生信息失败");
    }

    /**
     * 修改一条学生信息
     * @param student
     * @return
     */
    @PutMapping({"/update"})
    public Result updateStudent(Student student) {
        student.setUpdateTime(new Date());
        Boolean result = this.studentService.updateStu(student);
        return result ? Result.success("200", "修改学生信息成功", student) : Result.error("202", "修改学生信息失败");
    }

    /**
     * 删除一条学生信息
     * @param id
     * @return
     */
    @DeleteMapping({"/delete/{id}"})
    public Result deleteStudent(@PathVariable("id") int id) {
        Boolean result = this.studentService.deleteStu(id);
        return result ? Result.success("200", "删除学生信息成功", result) : Result.error("202", "删除学生信息失败");
    }
}
